package com.dqyy.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dqyy.order.entity.Order;
import com.dqyy.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping
    public Long createOrder(@RequestParam List<Long> dishIds, @RequestParam List<Integer> quantities) {
        return orderService.createOrder(dishIds, quantities);
    }

    @PostMapping("/{id}/pay")
    public boolean payOrder(@PathVariable Long id) {
        return orderService.payOrder(id);
    }

    @PostMapping("/{id}/cancel")
    public boolean cancelOrder(@PathVariable Long id) {
        return orderService.cancelOrder(id);
    }

    @GetMapping("/summary")
    public Map<String, Object> getOrderSummary() {
        return orderService.getOrderSummary();
    }

    @GetMapping("/list")
    public IPage<Order> getOrderList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer status) {
        log.info("分页查询参数 - page: {}, size: {}, status: {}", page, size, status);
        Page<Order> pageParam = new Page<>(page, size);
        IPage<Order> result = orderService.getOrderList(pageParam, status);
        log.info("分页查询结果 - 总记录数: {}, 总页数: {}, 当前页: {}, 每页大小: {}", 
            result.getTotal(), result.getPages(), result.getCurrent(), result.getSize());
        return result;
    }

    @GetMapping("/{id}")
    public Map<String, Object> getOrderDetail(@PathVariable Long id) {
        return orderService.getOrderDetail(id);
    }
} 